<?php
namespace app\models;
use ac\component\tool\Tool;
use app\models\Basic;

use Yii;

class Config extends Basic
{
    
    public static function tableName()
    {
        return '{{%sys_config}}';
    }

    public function rules()
    {
        return [
            ['company_name', 'required', 'message' => 'company_name不能为空'],
            ['company_name', 'string', 'message' => 'company_name必须是字符串',
                                'min' => 2, 'tooShort' => 'company_name最短2位',
                                'max' => 32, 'tooLong' => 'company_name最长32位'],
            ['company_logo', 'default', 'value' => ''],
            ['company_logo', 'string', 'message' => 'company_logo必须是字符串',
                                'min' => 2, 'tooShort' => 'company_logo最短2位',
                                'max' => 512, 'tooLong' => 'company_logo最长512位'],
            ['company_introduction', 'default', 'value' => ''],
            ['company_introduction', 'string', 'message' => 'company_introduction必须是字符串'],
            ['company_phone', 'default', 'value' => ''],
            ['company_phone', 'string', 'message' => 'company_phone必须是字符串',
                                'min' => 2, 'tooShort' => 'company_phone最短2位',
                                'max' => 128, 'tooLong' => 'company_phone最长128位'],
            ['photo_auth_cost', 'integer', 'message' => 'photo_auth_cost必须为数字，或数字字符串', 
                     'min'=>0, 'tooSmall' => 'photo_auth_cost最小存储0', 
                     'max'=>65535, 'tooBig' => 'photo_auth_cost最大存储65535'],
            ['mail_auth_cost', 'integer', 'message' => 'mail_auth_cost必须为数字，或数字字符串', 
                     'min'=>0, 'tooSmall' => 'mail_auth_cost最小存储0', 
                     'max'=>65535, 'tooBig' => 'mail_auth_cost最大存储65535'],
            ['expert_auth_cost', 'integer', 'message' => 'expert_auth_cost必须为数字，或数字字符串', 
                     'min'=>0, 'tooSmall' => 'expert_auth_cost最小存储0', 
                     'max'=>65535, 'tooBig' => 'expert_auth_cost最大存储65535'],
            ['record_info', 'default', 'value' => ''],
            ['record_info', 'string', 'message' => 'record_info必须是字符串'],
            ['register_info', 'default', 'value' => ''],
            ['register_info', 'string', 'message' => 'register_info必须是字符串'],
            ['lastmodify_time', 'default', 'value' => '1000-01-01 00:00:00'],
            ['lastmodify_time', 'date', 'format'=>'yyyy-MM-dd HH:mm:ss', 'message' => 'lastmodify_time非yyyy-MM-dd HH:mm:ss格式'],
        ];
    }


    /*添加*/
    public function addConfig($data)
    {
        $data['Config']['lastmodify_time'] = date('Y-m-d H:i:s', time());
        // P($data);
        if($this->load($data) and $this->validate()){
            if($this->save(false)){
                return true;
            }
        }
        return false;
    }

    /*
    修改
    $data       修改的数据
    $app_id 
    */
    public function modifyConfig($data)
    {
        if(isset($data['Config']['company_logo']) and is_array($data['Config']['company_logo'])){
            $data['Config']['company_logo'] = Tool::changeUploadFormat($data['Config']['company_logo']);
        }
        $config = self::find()->one();
        /*如果不存在，则需要添加一条*/
        if(empty($config)){
            $this->scenario = 'add';
            $config = new Config;
            // P($data);
            if($config->addConfig($data)){
                return true;
            }else{
                if($config->hasErrors()){
                    return $config->getErrors();
                }
            }
            return false;
        }
        if(!is_null($config)){
            if($this->load($data) and $this->validate()){
                if(!empty($this->company_name)){
                    $config->company_name = $this->company_name;
                }
                if(!empty($this->company_introduction)){
                    $config->company_introduction = $this->company_introduction;
                }
                if(!empty($this->company_logo)){
                    $config->company_logo = $this->company_logo;
                }
                if(!empty($this->company_phone)){
                    $config->company_phone = $this->company_phone;
                }
                if(!empty($this->photo_auth_cost)){
                    $config->photo_auth_cost = $this->photo_auth_cost;
                }
                if(!empty($this->mail_auth_cost)){
                    $config->mail_auth_cost = $this->mail_auth_cost;
                }
                if(!empty($this->expert_auth_cost)){
                    $config->expert_auth_cost = $this->expert_auth_cost;
                }
                if(!empty($this->record_info)){
                    $config->record_info = $this->record_info;
                }
                if(!empty($this->register_info)){
                    $config->register_info = $this->register_info;
                }
                $config->lastmodify_time = date('Y-m-d H:i:s', time());
                // P($config);
                if($config->save(false)){
                    return true;
                }
            }else{
                if($this->hasErrors()){
                    return $this->getErrors();
                }
            }
        };
        return false;
    }

    
}